package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.*;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {

        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        StrategyComment strategyComment = strategyCommentRepository.findById(id).orElse(null);
        return strategyComment;
    }

    @Override
    public List<StrategyComment> list() {

        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> queryPageList(StrategyCommentQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"_id");
        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }

    @Override
    public void commentThumb(String uid, String cid) {
        //首先判断评论类中的uid的List集合中是否有该id
        StrategyComment strategyComment = this.get(cid);
        if(strategyComment.getThumbuplist().contains(uid)){
            //包含用户id则说明已经点赞过,这次操作为取消点赞
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            strategyComment.getThumbuplist().remove(uid);
        }else{
            //不包含用户id则说明没有点赞过,这次操作为点赞
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            strategyComment.getThumbuplist().add(uid);
        }
        //更新数据
        this.update(strategyComment);
    }

}
